package com.jeff.android.lab.algorithm;

import android.widget.TextView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import java.io.File;

import butterknife.BindView;

/*
 * 冒泡排序(Bubble Sort)
 * 时间复杂度 O（n²）
 * */
public class FibonacciFragment extends BaseFragment {

    @BindView(R.id.tv_source)
    TextView tv_source;

    @BindView(R.id.tv_result)
    TextView tv_result;

    private int[] data = {49, 38, 65, 97, 76, 13, 27};

    @Override
    protected int getRootLayout() {
        return R.layout.algorithm_fragment_quick_sort;
    }

    @Override
    protected void initView() {
        super.initView();
//        print(tv_source);
    }

    @Override
    protected void initData() {

//        print(tv_result);

        tv_source.setText(Fibonacci1(12) + "");

        tv_result.setText(Fibonacci2(12) + " :: " + Fibonacci3(12));
    }


    private int Fibonacci1(int n) {
        int a = 1, b = 1, result = 0;
        if (n == 1 || n == 2) {
            result = 1;
        } else {
            for (int i = 3; i <= n; i++) {
                result = a + b;
                a = b;
                b = result;
            }
        }
        return result;
    }

    private int Fibonacci2(int n) {
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return Fibonacci2(n - 2) + Fibonacci2(n - 1);
        }
    }

    private int Fibonacci3(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == 0 || i == 1) {
                arr[i] = 1;
            } else {
                arr[i] = arr[i - 1] + arr[i - 2];
            }
        }
        return arr[n - 1];
    }


    private void print(TextView tv) {
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < data.length; i++) {
            sb.append(data[i]);
            if (i < data.length - 1)
                sb.append(",");
        }
        tv.setText(sb.toString());
    }

}
